<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head th:include="public/page_css::page_css ('查看-新增库位')" />

<body>

<form class="layui-form layui-card-header  layui-card-header-auto" id="searchForm">
        <div class="layui-input-inline" hidden>
            <input type="hidden" class="layui-input" th:value="${kwzid}" id="kwzid" name="kwzid"/>
        </div>
        <div class="layui-input-inline" hidden>
            <input type="hidden" class="layui-input" th:value="${kwzlx}" id="kwzlx" name="kwzlx"/>
        </div>

</form>
<section class="panel panel-padding layui-card-body">
    <div class="pro">
        <table id="table" class="table text-nowrap"></table>
    </div>
</section>
<div class="layui-form-item">
    <div class="layui-input-block">
        <button class="layui-btn" jq-filter="submit" lay-submit="" lay-filter="addKw">保存</button>
    </div>

</div>
</body>
<div th:include="public/public_js::public_js"></div>
<script>

    layui.use(['list','layer','zzpUtils','jquery','laydate'],function () {
        var zzpUtils= layui.zzpUtils,
            $ = layui.jquery,
            laydate = layui.laydate;
        var form = layui.form;
        window.operateEvent ={

        }
        var ids=[];
        var $table = zzpUtils.table({
            elem: '#table',
            isApi: true,
            pagination:true,
            url: '/wmslibrarypositiongroup/listkw',
            columns: [{
                field: '',
                checkbox: true,                        // 显示复选框
                formatter: function (i,row) {            // 每次加载 checkbox 时判断当前 row 的 id 是否已经存在全局 Set() 里
                    if ($.inArray(row.id, ids) != -1) {// 因为 判断数组里有没有这个 id
                        return {
                            checked: true               // 存在则选中
                        }
                    }
                }
            }, {
                field: 'mc',
                title: '库位名称'
            },{
                field: 'show_lx',
                title: '库位类型'
            },{
                field: 'cs',
                title: '库位层数'
            }],
            queryParams: function () {//查询条件
                return  zzpUtils.getFormJson($("#searchForm"));
            },
            detailFormatter: function (index, row) {
                return "这里就没有确定过11";
            },/*onClickRow:function ( row,index) {
                btn_cha(row.id);
            }*/
            onCheck: function (row) {
                btn_cha(row.id,1);
            },
            onUncheck: function (row) {
                btn_cha(row.id,1);
            },
            onCheckAll: function (rows) {
                $.each(rows, function (i, item) {
                    btn_cha(item.id,2);
                });

            },
            onUncheckAll: function (rows) {
                $.each(rows, function (i, item) {
                    btn_cha(item.id,3);
                });
            }
        });
        /*function btn_cha(id){
            if(ids.indexOf(id)>-1){
                var index = ids.indexOf(id);
                if (index > -1) {
                    ids.splice(index, 1);
                }
            }else{
                ids.push(id);
            }
        }*/
        //it 1 反选/勾选 ；2 全选；3全反选
        function btn_cha(id,it) {
            if (it == 1) {

                if (ids.indexOf(id) > -1) {
                    var index = ids.indexOf(id);
                    if (index > -1) {
                        ids.splice(index, 1);
                    }
                } else {
                    ids.push(id);
                }
            } else if (it == 3) {
                if (ids.indexOf(id) > -1) {
                    var index = ids.indexOf(id);
                    if (index > -1) {
                        ids.splice(index, 1);
                    }
                }
                /* else {
                 ids.push(id);
                 }*/

            } else{
                if (ids.indexOf(id) > -1) {
                    var index = ids.indexOf(id);
                    if (index > -1) {
                        /*ids.splice(index, 1);*/
                    }
                }else {
                    ids.push(id);
                }
            }
        }
         //保存库位
        form.on("submit(addKw)",function(data){
            var selectData= $('#table').bootstrapTable('getSelections');
            var list =[];
            $.each(ids, function (i, item) {
                list.push({
                    "id":item
                });
            });
            var data = {
                "id":$('#kwzid').val(),
                "list":list
            };
                zzpUtils.confirm("是否确定添加",function(index){
                    zzpUtils.action({
                        url:"/wmslibrarypositiongroup/editPosition",
                        data:JSON.stringify(data),
                        success:function (data) {
                            if(data.type=='success') {
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            }
                            setTimeout(function(){
                                top.layer.msg(data.title);
                            },500);
                        },error: function (xhr, textStatus) {
                            console.log(textStatus);
                            console.log(xhr);
                        }
                    })
                })
            return false;
        });
    });

</script>
</html>